/***************************************************************************************************
  Copyright(C)   2011-2013 Global Link Comm. Co., Ltd.
  All rights reserved.
  File Name: codegenerator.h

  Description: 一个串口类，TIS信息接收分析类

    用以接收和分析TIS通过串口发送过来的数据

   TIS通信协议文档：	H146 Software Design Document_rev2.pdf 6.2章节


  ***************************************************************************************************/
#ifndef TIS_H
#define TIS_H

#include <QObject>
#include <QDebug>
#include <QStringList>

#include "tis/tisserialport.h"
#include "public/configclass.h"
#include "InterfaceMsg.h"
#include "log.h"

#define DOORSTATUS       0x01 //(00000001)
#define PLATFORMSIDE     0x06 //(00000110)
#define CABSTATUS        0x08 //(00001000)
#define TRAVELINGSTATUS  0x80 //(10000000)

#define STATUS_CABOCCUPIED      1
#define STATUS_REST             2
#define STATUS_DOOROPEN         3
#define STATUS_DOORCLOSE        4
#define STATUS_TRAVELING        5
#define STATUS_PLATFORMDOOR     6
#define STATUS_CABUNOCCUPIED    7



typedef struct
{
    unsigned char status;           //状态
    unsigned char routeID;          //线路
    unsigned char stationID;        //当前站
    unsigned char destID;           //目的站
}tisNormalData;

typedef struct
{
    unsigned char spare1;
    unsigned char spare2;
    unsigned char spare3;
}tisSelfTestData;

typedef struct
{
    unsigned char blockNO[2];
    unsigned char block[1024];
}tisModifyProgData;

typedef struct
{
    unsigned char stx;
    unsigned char type;
    unsigned char cmd;
}tisHead;

typedef struct
{
    unsigned char etx;
    unsigned char hbcc;
    unsigned char lbcc;
}tisTail;

typedef struct
{
    tisHead       head;
    tisNormalData data;
    tisTail       tail;
}tisNormalPkg;

typedef struct
{
    tisHead       head;
    tisSelfTestData data;
    tisTail       tail;
}tisSelfTestPkg;

typedef struct
{
    tisHead       head;
    tisModifyProgData data;
    tisTail       tail;
}tisModifyProgPkg;

typedef struct
{
    tisHead       head;
    tisTail       tail;
}tisAnswerPkg;

extern Debug_Config_s g_stDebugConfig;

class Tis : public QObject
{
    Q_OBJECT

     LOG4QT_DECLARE_QCLASS_LOGGER

     enum ANS{
         Correct = 0x30,
         BccError,
         LengthError,
         FormatError,
         DisplayFault
     };

public:
     //nTisSeq,接收TIS时左边或右边对应的代码
     //nPidsSeq,回复TIS时左边或右边对应的代码

    explicit Tis(int nTisSeq = 0x40, int nPidsSeq = 0x50, QObject *parent = 0);
     ~Tis();

    typedef QextSerialBase::QueryMode QueryMode;
    qint64  Send(tisAnswerPkg/*pisToTisPkg*/* data);
    qint64  sendToTis(char ans);
    qint64  sendToTis(char type, char ans);
    bool    OpenPort(int nPort, ulong dwBaudRate, QueryMode mode = QextSerialBase::Polling);
    bool    OpenPort(/*const QString & name*/QString name, ulong dwBaudRate, QueryMode mode = QextSerialBase::Polling);
    void    closePort();
    void    setSeq(const int nTisSeq, const int nPidsSeq);

private:
    void  AnalyzeData(QByteArray& buf);
   //     void AnalyzeData(QByteArray* buf);
    bool  CheckBCC(QByteArray& buf);
    bool  CreateBCC(QByteArray& buf, unsigned char bcc[2]);
    void  SubmitMsg(tisToPisPkg *data);
    uchar TranslateStatus(uchar status);
    bool  TranslateStatus(uchar tisStatus, uchar& myStatus, uchar& platSide);

signals:
    void msgArrive(IMsg *data);
    void selfTest();

public slots:
    void recvFromTis(char *data, qint64 len);

private:
    TisSerialPort* m_com;
    QByteArray m_buf;

    uchar m_status;
    uchar m_statusID;

    //nTisSeq,m_nTisSeq,接收TIS时左边或右边对应的代码
    //nPidsSeq,m_nPidsSeq,回复TIS时左边或右边对应的代码
//    bool m_bFirstStation;
    int m_nTisSeq;
    int m_nPidsSeq;


    bool m_isTrunAround; //是否掉头

    int lastStationID;   //上一次的router
    int lastStatus;
    int lastRoutID;
};

#endif // TIS_H
